package com.youi.web;

import com.yeepay.shade.org.apache.commons.io.output.ByteArrayOutputStream;
import com.youi.common.Common;
import com.youi.common.DelStatus;
import com.youi.common.SessionKey;
import com.youi.entity.Activitygoods;
import com.youi.entity.Goods;
import com.youi.entity.Goodtype;
import com.youi.entity.Merchant;
import com.youi.entity.Operationlog;
import com.youi.entity.Sysuser;
import com.youi.exception.BusinessException;
import com.youi.manage.HttpRequestManage;
import com.youi.manage.HttpSessionManage;
import com.youi.model.GoodssizeObj;
import com.youi.model.ImgsObj;
import com.youi.service.ActivitygoodsService;
import com.youi.service.GoodsService;
import com.youi.service.GoodsimgsService;
import com.youi.service.GoodssizeService;
import com.youi.service.GoodtypeService;
import com.youi.service.MerchantService;
import com.youi.service.OperationlogService;
import com.youi.third.wechat.model.AccessToken;
import com.youi.third.wechat.util.MiniAppPoster;
import com.youi.third.wechat.util.WechatUtil;
import com.youi.util.CommonInfoUtil;
import com.youi.util.FileUploadUtil;
import com.youi.util.IpUtil;
import com.youi.util.JsonUtil;
import com.youi.util.ObjectUtil;
import com.youi.util.Page;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import java.awt.image.BufferedImage;
import java.io.InputStream;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * 商品信息控制层
 *
 * @author Administrator
 */
@Controller
public class GoodsController extends HttpRequestManage implements ApplicationContextAware {

    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsimgsService goodsimgsService;
    @Autowired
    private GoodtypeService goodtypeService;
    @Autowired
    private GoodssizeService goodssizeService;
    @Autowired
    private MerchantService merchantService;
    @Autowired
    private ActivitygoodsService activitygoodsService;
    @Autowired
    private OperationlogService operationlogService;

    /**
     * 同步静态对象 零长度的byte数组对象创建起来将比任何对象都经济――查看编译后的字节码：生成零长度的byte[]对象只需3条操作码，而Object lock = new Object()则需要7行操作码
     */
    private static byte[] lock = null;

    /**
     * I-16
     * 添加或编辑商品信息
     *
     * @param modelMap
     * @param request
     * @return
     * @throws BusinessException
     * @throws ParseException
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/addOrEditGoods")
    public String addOrEditGoods(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        synchronized (lock) {
            Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
            Long id = optLong("id", request);
            Goods goods = null;
            if (id != null) {
                goods = goodsService.get(id);
            } else {
                if (sysuser.getType() == 1) {
                    Map<String, Object> params = new HashMap<String, Object>();
                    params.put(Common.DELSTATUS, DelStatus.NODEL);
                    params.put(Common.ID, sysuser.getMerchantId());
                    Merchant merchant = merchantService.getByParam(params);
                    if (merchant == null) {
                        return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "商家不存在");
                    }
                    params = new HashMap<String, Object>();
                    params.put(Common.DELSTATUS, DelStatus.NODEL);
                    params.put("merchantId", merchant.getId());
                    Integer goodsCount = goodsService.getCount(params);
                    if (goodsCount >= merchant.getGoodsNum()) {
                        return JsonUtil.toJson(CommonInfoUtil.OBJECTEXIST, "上传商品数量达到上限");
                    }
                }
                goods = new Goods();
                goods.setDelStatus(DelStatus.NODEL);
                goods.setGmtCreated(new Date());
                goods.setStatus(0);
                goods.setApplyStatus(0);
            }
            if (sysuser.getType() == 1) {
                goods.setMerchantId(sysuser.getMerchantId());
            } else {
                Long merchantId = optLong("merchantId", request);
                if (merchantId != null) {
                    goods.setMerchantId(merchantId);
                }
            }
            Long goodsTypeId = getLong("goodsTypeId", request);
            if (goodsTypeId != null) {
                goods.setGoodsTypeId(goodsTypeId);
            }
            String name = optString("name", request);
            if (name != null) {
                goods.setName(name);
            }
            String internationalNo = optString("internationalNo", request);
            if (internationalNo != null) {
                goods.setInternationalNo(internationalNo);
            }
            String subhead = optString("subhead", request);
            if (subhead != null) {
                goods.setSubhead(subhead);
            }
            String shortTitle = optString("shortTitle", request);
            if (shortTitle != null) {
                goods.setShortTitle(shortTitle);
            }
            String keyword = optString("keyword", request);
            if (keyword != null) {
                goods.setKeyword(keyword);
            }
            Integer type = optInteger("type", request, 0);
            if (type != null) {
                goods.setType(type);
            }
            Integer presell = optInteger("presell", request, 0);
            if (presell != null) {
                goods.setPresell(presell);
            }
            String goodsType = optString("goodsType", request);
            if (goodsType != null) {
                goods.setGoodsType(goodsType);
            }
            String attribute = optString("attribute", request);
            if (attribute != null) {
                goods.setAttribute(attribute);
            }
            String specification = optString("specification", request);
            if (specification != null) {
                goods.setSpecification(specification);
            }
            Double ecPrice = optDouble("ecPrice", request);
            if (ecPrice != null) {
                goods.setEcPrice(ecPrice);
            }
            Double slaePrice = optDouble("slaePrice", request);
            if (slaePrice != null) {
                goods.setSlaePrice(slaePrice);
            }
            Double originalPrice = optDouble("originalPrice", request);
            if (originalPrice != null) {
                goods.setOriginalPrice(originalPrice);
            }
            Double costPrice = optDouble("costPrice", request);
            if (costPrice != null) {
                goods.setCostPrice(costPrice);
            }
            Integer realSaleCnt = optInteger("realSaleCnt", request);
            if (realSaleCnt != null) {
                goods.setRealSaleCnt(realSaleCnt);
            }
            Integer realStockCnt = optInteger("realStockCnt", request);
            if (realStockCnt != null) {
                goods.setRealStockCnt(realStockCnt);
            }
            String param = optString("param", request);
            if (param != null) {
                goods.setParam(param);
            }
            String details = optString("details", request);
            if (details != null) {
                goods.setDetails(details);
            }
            Integer topShow = optInteger("topShow", request, 1);
            if (topShow != null) {
                goods.setTopShow(topShow);
            }
            Date gmtStart = optDate("gmtStart", request, "yyyy-MM-dd HH:mm:ss");
            if (gmtStart != null) {
                goods.setGmtStart(gmtStart);
            }
            Date gmtEnd = optDate("gmtEnd", request, "yyyy-MM-dd HH:mm:ss");
            if (gmtEnd != null) {
                goods.setGmtEnd(gmtEnd);
            }
            goods.setGmtModify(new Date());
            goodsService.saveOrUpdate(goods);
            //商品二维码
            if (goods.getAddressUrl() == null) {
                goods.setAddressUrl("pages/product/product?id=" + goods.getId().toString());
            }
            //首页视频
            String vedio = optString("vedio", request);
            goodsimgsService.addOrGoodsImg(id, goods, 0, vedio);
            //首页图
            String indexImg = optString("indexImg", request);
            goodsimgsService.addOrGoodsImg(id, goods, 1, indexImg);

            //轮播图
            String banner = optString("banner", request);
            List<ImgsObj> imgsObjList = JsonUtil.fromArrayJson2List(banner, ImgsObj.class);
            goodsimgsService.addOrGoodsImg(id, goods, 2, imgsObjList);
            //商品规格
            String goodsSize = optString("goodsSize", request);
            if (goodsSize != null && goodsSize.length() > 0) {
                List<GoodssizeObj> goodssizeList = JsonUtil.fromArrayJson2List(goodsSize, GoodssizeObj.class);
                if (goodssizeList != null && goodssizeList.size() > 0) {
                    Integer result = goodssizeService.addGoodssize(id, goods, goodssizeList);
                    if (result == 0) {
                        return JsonUtil.toJson(CommonInfoUtil.NEEDPERMISSIONS, "录入库存不能小于销量");
                    }
                }
            }
            if (goods.getGoodsNo() == null || goods.getGoodsNo().length() == 0) {
                Map<String, Object> params = new HashMap<String, Object>();
                params.put(Common.DELSTATUS, DelStatus.NODEL);
                Integer count = goodsService.getCount(params);
                Goodtype goodtype = goodtypeService.get(goods.getGoodsTypeId());
                goods.setGoodsNo(goodtype.getCode() + ObjectUtil.getCode(String.valueOf(goods.getId())));
                goodsService.saveOrUpdate(goods);
            }
            Map<String, Object> prams = new HashMap<String, Object>();
            prams.put(Common.DELSTATUS, DelStatus.NODEL);
            prams.put("goodsId", goods.getId());
            Activitygoods activitygoods = activitygoodsService.getByParam(prams);
            if (activitygoods != null) {
                activitygoods.setGmtModify(new Date());
                activitygoods.setDelStatus(DelStatus.DEL);
                activitygoodsService.saveOrUpdate(activitygoods);
            }
            Integer sort = optInteger("sort", request);
            if (sort != null) {
                if (goods.getSort() != null) {
                    if (sort.equals(goods.getSort())) {
                        goods.setStatus(0);
                        goods.setApplyStatus(0);
                    }
                }
                goods.setSort(sort);
            }
            goodsService.saveOrUpdate(goods);
            String ip = IpUtil.getIpAddr(request);
            if (id == null) {
                operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "添加了商品" + goods.getName()));
            } else {
                operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "修改了了商品" + goods.getName()));
            }
            return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
        }

    }

    /**
     * I-19
     * 批量删除商品信息
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/delGoodsByIds")
    public String delGoodsByIds(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        String ids = getString("ids", request);
        List<Long> idsList = ObjectUtil.getIdListByIds(ids);
        if (idsList == null || idsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.ID, idsList);
        if (sysuser.getMerchantId() != null) {
            params.put("merchantId", sysuser.getMerchantId());
        }
        List<Goods> goodsList = goodsService.getByParams(params);
        if (goodsList == null || goodsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        String ip = IpUtil.getIpAddr(request);
        for (Goods goods : goodsList) {
            goods.setDelStatus(DelStatus.DEL);
            operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "删除了商品编号为:" + goods.getGoodsNo() + "的商品"));
        }
        goodsService.saveOrUpdateAll(goodsList);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }

    /**
     * I-43
     * 批量操作商品信息
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/editGoodsByIds")
    public String editGoodsByIds(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        String ids = getString("ids", request);
        Integer status = optInteger("status", request);
        Integer topShow = optInteger("topShow", request);
        Integer applyStatus = optInteger("applyStatus", request);
        List<Long> idsList = ObjectUtil.getIdListByIds(ids);
        if (idsList == null || idsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.PARAMERROR, "ids参数错误");
        }
        Map<String, Object> params = new HashMap<String, Object>();
        params.put(Common.DELSTATUS, DelStatus.NODEL);
        params.put(Common.ID, idsList);
        if (sysuser.getMerchantId() != null) {
            params.put("merchantId", sysuser.getMerchantId());
        }
        List<Goods> goodsList = goodsService.getByParams(params);
        if (goodsList == null || goodsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        goodsService.getGoodsMsg(goodsList);
        for (Goods goods : goodsList) {
            if (status != null) {
                if (goods.getStatus() == 0 && goods.getApplyStatus() == 0) {
                    return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "商品未审核或审核不通过无法上架");
                }
                if (goods.getApplyStatus() == -1) {
                    return JsonUtil.toJson(CommonInfoUtil.NOOPPMS, "商品审核未通过无法上架");
                }
                if (goods.getGoodssizeList() == null || goods.getGoodssizeList().size() == 0) {
                    return JsonUtil.toJson(CommonInfoUtil.NOPERMISSIONS, "商品信息未全无法上架");
                }
                if (status == 1) {
                    if (goods.getGmtStart().getTime() > System.currentTimeMillis()) {
                        status = 4;
                    } else if (goods.getGmtEnd().getTime() < System.currentTimeMillis()) {
                        status = 5;
                    }
                    //上架时,设置上架时间
					goods.setGmtOnSale(new Date());
                } else if (status == 3) {
                    Map<String, Object> prams = new HashMap<String, Object>();
                    prams.put(Common.DELSTATUS, DelStatus.NODEL);
                    prams.put("goodsId", goods.getId());
                    Activitygoods activitygoods = activitygoodsService.getByParam(prams);
                    if (activitygoods != null) {
                        activitygoods.setGmtModify(new Date());
                        activitygoods.setDelStatus(DelStatus.DEL);
                        activitygoodsService.saveOrUpdate(activitygoods);
                    }
                }
                goods.setStatus(status);
            }
            if (topShow != null) {
                goods.setTopShow(topShow);
            }
            if (applyStatus != null) {
                goods.setApplyStatus(applyStatus);
                goods.setStatus(3);
            }
            String ip = IpUtil.getIpAddr(request);
            if (status != null) {
                operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "修改商品编号为:" + goods.getGoodsNo() + "的商品状态为:" + ObjectUtil.getGoodsStaus(status)));
            }
            if (topShow != null) {
                operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "修改商品编号为:" + goods.getGoodsNo() + "商品首页显示状态"));
            }
            if (applyStatus != null) {
                operationlogService.saveOrUpdate(new Operationlog(sysuser.getId(), ip, IpUtil.getAddresses(ip), "审核了商品编号为:" + goods.getGoodsNo() + "的商品审核状态为:" + ObjectUtil.getGoodsAuditStaus(applyStatus)));
            }
        }
        goodsService.saveOrUpdateAll(goodsList);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成");
    }

    /**
     * I-17
     * 查询商品信息列表
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/getGoods")
    public String getGoods(ModelMap modelMap, HttpServletRequest request) throws BusinessException, ParseException {
        Sysuser sysuser = (Sysuser) HttpSessionManage.getAttribute(request, SessionKey.SYSUSER);
        Integer pageIndex = optInteger("pageIndex", request, 1);
        Integer pageNum = optInteger("pageNum", request, 10);
        Integer status = optInteger(Common.STATUS, request);
        Integer delStatus = optInteger(Common.DELSTATUS, request);
        String des = optString("des", request, Common.DESC);
        String pms = optString("pms", request);
        String typePms = optString("typePms", request);
        //商品属性
        String attribute = optString("attribute", request);
        //商品分类id
        Long goodsTypeId = optLong("goodsTypeId", request);
        Date start = optDate("start", request, "yyyy-MM-dd HH:mm:ss");
        Date end = optDate("end", request, "yyyy-MM-dd HH:mm:ss");
        String goodsCodes = optString("goodsCodes",request);
        Map<String, Object> prams = new HashMap<String, Object>();
        prams.put(Common.DELSTATUS, DelStatus.NODEL);
        if (sysuser.getMerchantId() != null) {
            prams.put("merchantId", sysuser.getMerchantId());
        }
        if (status != null) {
            if (status == 1) {
                prams.put(Common.STATUS, Arrays.asList(1, 4, 5));
            } else {
                prams.put(Common.STATUS, status);
            }
        }
        if (goodsTypeId != null) {
            prams.put("goodsTypeId", goodsTypeId);
        }
        if(goodsCodes!=null){
            if(!goodsCodes.equals("")){
                prams.put("goodsNo", ObjectUtil.getIdListByIds(goodsCodes));
            }
        }
        Map<String, Object> sortPram = new HashMap<String, Object>();
        sortPram.put(Common.GMTMODIFY, des);
        sortPram.put("sort", des);
        Map<String, Object> searchPram = null;
        if (pms != null) {
            searchPram = new HashMap<String, Object>();
            searchPram.put("id", pms);
            searchPram.put("name", pms);
            searchPram.put("goodsNo", pms);
        }
        Map<String, Object> keyParams = null;
        if (attribute != null && attribute.length() > 0) {
            keyParams = new HashMap<String, Object>();
            keyParams.put("attribute", attribute);
        }
        Map<String, Object> startTime = null;
        if (start != null) {
            startTime = new HashMap<String, Object>();
            startTime.put(Common.GMTCREATED, start);
        }
        Map<String, Object> endTime = null;
        if (end != null) {
            endTime = new HashMap<String, Object>();
            endTime.put(Common.GMTCREATED, end);
        }
        Map<String,Object> noParams = null;
        String noIds = optString("noIds",request);
        if(noIds==null){
            noIds = "";
        }
        if(!noIds.equals("")){
            noParams = new HashMap();
            noParams.put("id",ObjectUtil.getIdListByIds(noIds));
        }

        //类别搜索
        Map<String, Object> typeSearchPram = null;
        if (typePms != null) {
            typeSearchPram = new HashMap<String, Object>();
            typeSearchPram.put("name", typePms);
            typeSearchPram.put("code", typePms);

            List<Goodtype> goodtypeList = goodtypeService.getByParamsList(null,typeSearchPram,null,null,null,null,null);
            List<Long> typeIds = new ArrayList();
            for(Goodtype goodtype:goodtypeList){
                typeIds.add(goodtype.getId());
            }
            typeIds.add(Long.valueOf(-1));
            prams.put("goodsTypeId",typeIds);
        }

//        List<Goods> goodsList = goodsService.getGoodsList(prams, searchPram, keyParams, sortPram, (pageIndex - 1) * pageNum, pageNum, startTime, endTime);
        List<Goods> goodsList = goodsService.getGoodsListNotInIds(prams,noParams, searchPram, keyParams, sortPram, (pageIndex - 1) * pageNum, pageNum, startTime, endTime);
        if (goodsList == null || goodsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
//        Integer pageCount = goodsService.getCountByParamss(prams, searchPram, startTime, endTime);
        Integer pageCount = goodsService.getGoodsListNotInIds(prams,noParams, searchPram, null, null, null, null, startTime, startTime).size();
        modelMap.put(CommonInfoUtil.PAGECOUNT, Page.getCount(pageCount, pageNum));//pageCount  总页数
        modelMap.put(CommonInfoUtil.PAGEINDEX, pageIndex);            //pageIndex     查询页数
        modelMap.put(CommonInfoUtil.ALLCOUINT, pageCount);
        modelMap.put(CommonInfoUtil.DATA, goodsList);        //jsonObjectList json对象集合
        goodsService.getGoodsMsg(goodsList);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", modelMap);
    }

    /**
     * I-18
     * 查询商品信息详情
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/getGoodsById")
    public String getGoodsById(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        Long id = getLong("id", request);
        Goods goods = goodsService.get(id);
        if (goods == null) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        goodsService.getGoodsMsg(goods);
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", goods);//jsonObject	json对象
    }

    /**
     * I-64
     * 查询商品二维码链接
     *
     * @throws Exception
     */
    @ResponseBody
    @RequestMapping("sysUserGetInfo/getGoodsUrlById")
    public String getGoodsUrlById(ModelMap modelMap, HttpServletRequest request) throws Exception {
        Long id = getLong("id", request);
        Goods goods = goodsService.get(id);
        if (goods == null) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "商品不存在");
        }
        if (goods.getAddressUrl() == null) {
            goods.setAddressUrl("pages/product/product?id=" + goods.getId().toString());
        }
        AccessToken accessToken = WechatUtil.weChatMiniGetAccessToken();
        if (accessToken == null) {
            return JsonUtil.toJson(CommonInfoUtil.NULLOBJECT, "accesstoken获取失败");
        }
        BufferedImage qrCode = MiniAppPoster.getQrCode(accessToken.getAccess_token(), goods.getAddressUrl(), 280);
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        ImageIO.write(qrCode, "jpg", os);
        byte[] bs = readInputStream(os.toInputStream());
        String pathUrl = request.getSession().getServletContext().getRealPath("/");
        String returnUrlStr = FileUploadUtil.fileUpload(pathUrl, "code.jpg", bs);
        String requestUrl = "http://" + request.getServerName() + request.getContextPath();
        String url = requestUrl + "/" + returnUrlStr;
        modelMap.put("addressUrl", goods.getAddressUrl());
        modelMap.put("url", url);
        modelMap.put(CommonInfoUtil.JSONCODE, CommonInfoUtil.SUCCESS);
        modelMap.put(CommonInfoUtil.JSONMSG, "操作成功");
        return JsonUtil.toJson(modelMap);//jsonObject	json对象
    }

    /**
     * 查询所有商品信息
     *
     * @param modelMap
     * @param request
     * @return
     */
    @ResponseBody
    @RequestMapping("getAllGoods")
    public String getAllGoods(ModelMap modelMap, HttpServletRequest request) throws BusinessException {
        List<Goods> goodsList = goodsService.getByParams(null);
        if (goodsList == null || goodsList.size() == 0) {
            return JsonUtil.toJson(CommonInfoUtil.NULLDATA, "空数据");
        }
        return JsonUtil.toJson(CommonInfoUtil.SUCCESS, "操作完成", goodsList);//jsonObjectList	json对象集合
    }

    /**
     * 定时器线程池
     */
    private static ScheduledThreadPoolExecutor executor;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        // TODO Auto-generated method stub
        if (lock == null) {
            lock = new byte[0];
            executor = new ScheduledThreadPoolExecutor(1);
        }
    }

    private byte[] readInputStream(InputStream inStream) throws Exception {
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        //创建一个Buffer字符串
        byte[] buffer = new byte[1024];
        //每次读取的字符串长度，如果为-1，代表全部读取完毕
        int len = 0;
        //使用一个输入流从buffer里把数据读取出来
        while ((len = inStream.read(buffer)) != -1) {
            //用输出流往buffer里写入数据，中间参数代表从哪个位置开始读，len代表读取的长度
            outStream.write(buffer, 0, len);
        }
        //关闭输入流
        inStream.close();
        //把outStream里的数据写入内存
        return outStream.toByteArray();
    }

}
